//
//  AppDelegate.m
//  何五路
//
//  Created by _ADY on 15/6/5.
//  Copyright (c) 2015年 _ADY. All rights reserved.
//

#import "AppDelegate.h"
#import "MyNavigationController.h"
#import "ViewController.h"
#import "HomeViewController.h"
#import "ExperienceViewController.h"
#import "ShoppingViewController.h"
#import "MyViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [application setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
    [SendService customViewDidLoad];
    [SendService setSomething];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
    hostReach = [Reachability reachabilityWithHostName:@"www.google.com"];
    [hostReach startNotifier];
    
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"])
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        [self gotoHome];
    }
    else
    {
        [self gotoHome];
    }
    //    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    
    //消息推送注册
    //    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge];
    
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isPayView"];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isLoginKey"];
    [self.window makeKeyAndVisible];
    
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths  objectAtIndex:0];
    NSString *filename=[path stringByAppendingPathComponent:@"cookie.plist"];
    NSMutableDictionary *remData= [NSMutableDictionary dictionaryWithContentsOfFile:filename];
    if (remData != nil)
    {
        [remData removeAllObjects];
        [remData writeToFile:filename atomically:YES];
    }
    
    return YES;
}

-(void)gotoHome
{
    
    HomeViewController* vc1 = [[HomeViewController alloc] init];
    ExperienceViewController* vc2= [[ExperienceViewController alloc] init];
    ShoppingViewController* vc3 = [[ShoppingViewController alloc] init];
    MyViewController* vc4 = [[MyViewController alloc] init];
//    MyNavigationController
    UINavigationController* nav1 = [[UINavigationController alloc] initWithRootViewController:vc1];
    UINavigationController* nav2 = [[UINavigationController alloc] initWithRootViewController:vc2];
    UINavigationController* nav3 = [[UINavigationController alloc] initWithRootViewController:vc3];
    UINavigationController* nav4 = [[UINavigationController alloc] initWithRootViewController:vc4];
    
    
    [nav1.tabBarItem setImage:[UIImage imageNamed:@"gnobg_menu1"]];
    [nav2.tabBarItem setImage:[UIImage imageNamed:@"gnobg_menu2"]];
    [nav3.tabBarItem setImage:[UIImage imageNamed:@"gnobg_menu3"]];
    [nav4.tabBarItem setImage:[UIImage imageNamed:@"gnobg_menu4"]];
    NSArray* viewControllers = [[NSArray alloc] initWithObjects:nav1, nav2, nav3, nav4, nil];
    
    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    [tabBarController setViewControllers:viewControllers];
    self.window.rootViewController = tabBarController;
}

- (void)reachabilityChanged:(NSNotification *)note {
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
    NetworkStatus status = [curReach currentReachabilityStatus];
    
    NSString *sting = nil;
//    isWifi = NO;
    if (status == NotReachable) {
        sting = @"无网络连接!";
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络不可用,请检查网络连接!"  delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
    }
    else if (status == kReachableViaWWAN)
    {
        sting = @"已切换至移动网络!";
    }
    else if (status == kReachableViaWiFi)
    {
        sting = @"已切换至Wi-Fi !";
//        isWifi = YES;
    }
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
